package com.travel.common.utils;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.travel.document.entity.Document;
import com.travel.document.service.DocumentService;
import com.travel.document.service.impl.DocumentServiceImpl;

import lombok.extern.slf4j.Slf4j;

/**
 * @Description 操作文件工具类
 * @author LJ
 * @Date 2016年6月8日 上午11:42:44
 * @Version v1.0
 */
@Slf4j
@Component
public class OperationFileUtil {

	private static DocumentService documentService;

	@Autowired
	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
	}

	/**
	 * 单个文件上传
	 * 
	 * @param file
	 * @param request
	 * @return
	 */
	public static boolean uploadFile(MultipartFile file, HttpServletRequest request, String imgPath) {
		log.info("当前路径是:" + imgPath);
		String fileName = file.getOriginalFilename();
		File dest = new File(imgPath + fileName);
		try {
			file.transferTo(dest);
			log.info("上传成功");
			return true;
		} catch (IOException e) {
			log.error(e.toString(), e);
		}
		return false;
	}

	/**
	 * 多个文件上传
	 */

	public static boolean uploadFiles(HttpServletRequest request, String imgPath, Long objId) {
		List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");

		if (files == null || files.size() <= 0) {
			return false;
		}
		log.info("当前上传的文件数目有:" + files.size());
		for (int i = 0; i < files.size(); i++) {
			MultipartFile file = files.get(i);
			if (file.isEmpty()) {
				log.error("上传第" + (i++) + "个文件失败");
				continue;
			}

			String fileName = OrderNoUtils.sdf.format(new Date()) + ".jpg";

			// 获取当前新的图片路径
			String path = imgPath + fileName;
			File dest = new File(path);

			try {
				file.transferTo(dest);
				log.info("第" + (i + 1) + "个文件上传成功");
				// 创建一个文件管理的，
				Document doc = new Document(objId, path);
				System.out.println(documentService);
				documentService.addFile(doc);
			} catch (IOException e) {
				log.error(e.toString(), e);
				log.error("上传第" + (i++) + "个文件失败");
				return false;
			}
		}
		return true;
	}

}